package com.molichuxing.gateway.api.services.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.SmsApiService;
import com.molichuxing.gateway.bff.user.entity.request.create.PictureCreateVo;
import com.molichuxing.gateway.bff.user.entity.request.create.SmsCreateVo;
import com.molichuxing.gateway.bff.user.services.SmsBffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author liyg
 * 短信网关接口实现
 * 2019-09-18
 */
@Api(tags = "短信相关接口")
@RestController
@RequestMapping(value = "/sms")
public class SmsApiServiceImpl extends RootApiService implements SmsApiService{

	private static final Logger logger = LoggerFactory.getLogger(SmsApiServiceImpl.class);
	
	@Resource
	private SmsBffService smsBffService;
	
	
	/**
	 * 发送短信code码
	 * @param createVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "发送短信code码")
	@PostMapping(value = "/send")
	public ResponseResult<Boolean> createSms(@RequestBody SmsCreateVo createVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			Boolean flag = smsBffService.createSms(createVo);
			if(!flag) {
				result.error("发送短信验证码失败");
			}
			return result.success(flag);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (Exception e) {
			logger.error("[createSms]发送短信code码异常", e);
			result.error("发送短信code码异常");
		}
		return result;
	}
	
	/**
	 * 获取图形验证码
	 * @param createVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取图形验证码")
	@PostMapping(value = "/picture/code")
	public ResponseResult<String> createPictureCode(@RequestBody PictureCreateVo createVo){
		ResponseResult<String>  result = new ResponseResult<>();
		try {
			String codePic = smsBffService.createPictureCode(createVo);
			return result.success(codePic);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (Exception e) {
			logger.error("[createPictureCode]获取图形验证码异常", e);
			result.error("获取图形验证码异常",null);
		}
		return result;
	}

}
